package ${baseInfo.packageName};

import com.fa4j.common.base.exception.CommonRequestError;
import ${repositoryInterface.packageName}.${repositoryInterface.fileName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import ${converter.packageName}.${converter.fileName};

import com.fa4j.common.base.model.RemoveCmd;
import ${domain.packageName}.${domain.fileName};
import ${createCmd.packageName}.${createCmd.fileName};
import ${modifyCmd.packageName}.${modifyCmd.fileName};

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * ${tableClass.remark} 服务接口实现
 *
 * @since ${.now?string('yyyy-MM-dd HH:mm:ss')}
 */
@Slf4j
@Service
@Validated
@RequiredArgsConstructor
public class ${baseInfo.fileName} implements ${serviceInterface.fileName}{

    private final ${repositoryInterface.fileName} ${repositoryInterface.fileName?uncap_first};
    private final ${converter.fileName} ${converter.fileName?uncap_first};

    /**
     * 新增${tableClass.remark}
     *
     * @param cmd 新增参数
     * @return ${tableClass.remark}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${domain.fileName} create(@Valid ${createCmd.fileName} cmd) {
        ${domain.fileName} entity = ${converter.fileName?uncap_first}.toEntity(cmd);
        ${repositoryInterface.fileName?uncap_first}.create(entity);
        return entity;
    }

    /**
     * 编辑${tableClass.remark}
     *
     * @param cmd 编辑参数
     * @return ${tableClass.remark}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${domain.fileName} modify(@Valid ${modifyCmd.fileName} cmd) {
        ${domain.fileName} entity = ${repositoryInterface.fileName?uncap_first}.findById(cmd.getId())
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("${tableClass.remark}"));
        ${converter.fileName?uncap_first}.updateEntity(entity, cmd);
        ${repositoryInterface.fileName?uncap_first}.modify(entity);
        return entity;
    }

    /**
     * 删除${tableClass.remark}
     *
     * @param cmd 删除参数
     * @return ${tableClass.remark}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${domain.fileName} remove(@Valid RemoveCmd<${tableClass.pkFields[0].shortTypeName}> cmd) {
        ${domain.fileName} entity = ${repositoryInterface.fileName?uncap_first}.findById(cmd.getId())
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("${tableClass.remark}"));
        entity.setOperatorId(cmd.getOperatorId());
        ${repositoryInterface.fileName?uncap_first}.remove(entity);
        return entity;
    }

}




